package com.example.jd_flutter.jdPlugin;


import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;


import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;


import com.example.jd_flutter.MainActivity;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.Result;


public class JDPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {
    private final MainActivity mActivity;

    public JDPlugin(MainActivity activity) {
        this.mActivity = activity;
    }

    private MethodChannel channel;

    private JDPlugin currentPlugin;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
        Log.e("安卓 onAttachedToEngine", "111");

        BinaryMessenger messenger = binding.getBinaryMessenger();
        channel = new MethodChannel(messenger, "jd_learn_plugin");
        channel.setMethodCallHandler(this);
        currentPlugin = this;
        this.mActivity.currentPlugin = currentPlugin;
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
        channel = null;
        currentPlugin = null;
        this.mActivity.currentPlugin = null;
    }

    //flutter调用安卓
    @Override
    public void onMethodCall(final MethodCall call, final Result result) {
        Log.e("收到原生调用11 onMethodCall", call.method);
        if ("getAppInfo".equals(call.method)) {
            //获取app信息
            try {
                getAppInfo(call.arguments, result);
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(e);
            }
        } else {
            if (result != null) {
                result.notImplemented();
            }
        }
    }


    private void getAppInfo(Object args, Result result) throws PackageManager.NameNotFoundException {
        // 获取设备信息
        if (result != null) {
            Map<String,String> map = new HashMap();
            PackageManager packageManager = this.mActivity.getApplicationContext().getPackageManager();
            // 获取当前包的信息
            PackageInfo packageInfo = packageManager.getPackageInfo(this.mActivity.getApplicationContext().getPackageName(), 0);
            // 获取版本名称和版本号
            String versionName = packageInfo.versionName;
            map.put("version", versionName);
            map.put("buildNumber", getLongVersionCode(packageInfo));
            Log.d("安卓 回调给 flutter", map.toString());
            result.success(map);
        }
    }

    String getLongVersionCode(PackageInfo info ) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
             return  Long.toString(info.getLongVersionCode());
        } else {
             return String.valueOf(info.versionCode);
        }
    }

    //安卓调用flutter
    private void invokeMethod(String method, Object args) {
        currentPlugin.channel.invokeMethod(method, args);
    }

}


